<?php

namespace App\Admin\Controllers\Goods;

use App\Admin\Forms\Order\BtcOrder;
use App\Admin\Forms\Order\EthOrder;
use App\Admin\Forms\Order\FilOrder;
use App\Admin\Forms\Order\OrderPower;
use App\Admin\Forms\Order\ReturnDiya;
use App\Models\Goods\Product;
use App\Admin\Repositories\Goods\UserProduct;
use App\Models\Partner\UserProductPartner;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Admin;

class UserProductController extends AdminController
{


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserProduct(['user']), function (Grid $grid) {
            $grid->column('id');
            $grid->column("product_id");
            $grid->column("product_title");
            $grid->column("user_id");
            $grid->column("user.username");
            $grid->column('buy_shares');
            $grid->column("dig_coin");
            $grid->column("amount");
            $grid->column("pay_coin");
            $grid->column("tech_fee");
            $grid->column("created_at");
            $grid->column('produce_at');
            $grid->column('product_expires_at');
            $grid->column("phs_ratio");
            $grid->column("status");
            $grid->column("extend");

            $grid->column("user_product_power");
            $grid->model()->orderBy("id",'desc');
            $grid->setActionClass(\Dcat\Admin\Grid\Displayers\DropdownActions::class);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserProduct(['user']), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('product_id');
            $show->field('order_sn');
            $show->field('product_title');
            $show->field('pay_coin');
            $show->field('buy_shares');
            $show->field('buy_price');
            $show->field('amount');
            $show->field('pay_amount');
            $show->field('status');
            $show->field('total_phs');
            $show->field('tt');
            $show->field('custody_fee');
            $show->field('tech_fee');
            $show->field('produce_at');
            $show->field('phs_ratio');
            $show->field('amount_cny');
            $show->field('pay_type');
            $show->field('dig_coin');
            $show->field('custody_expires_at');
            $show->field('product_expires_at');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableDeleteButton();
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserProduct(), function (Form $form) {
            $form->select('status');
            $form->text("buy_shares");
            $form->text('pay_amount');
            $form->text('amount');
            $form->text("tech_fee");
            $form->text("phs_ratio");
            $form->text("produce_at");
            $form->text("product_expires_at");
            $form->saved(function (Form $form){
                $id = $form->getKey();
                $techFee = $form->input('tech_fee');
                $produceAt = strtotime($form->input('produce_at'));
                $productExpiresAt = strtotime($form->input('product_expires_at'));
                if($id){
                    $model = new \App\Models\Goods\UserProduct();
                    $order = $model->where('id',$id)->first();
                        $model::where('id',$id)->update([
                        'tech_fee'=> !empty($techFee) ? $techFee : $order->tech_fee,
                        'produce_at' => !empty($produceAt) ? $produceAt : $order->produce_at,
                        'product_expires_at' => !empty($productExpiresAt) ? $productExpiresAt : $order->product_expires_at
                    ]);
                }
            });
        });
    }
}
